﻿
@{
    ViewBag.Title = "MaterialView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}



<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>协议库存</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入规格名称" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            <div class="layui-form-mid layui-word-aux">高级搜索</div>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="UpLoadExcel"><i class="layui-icon">&#xe61f;</i>导入表格</button>

        </div>
    </div>
</form>
<table id="demo" lay-filter="ProjectTableList"></table>

<div id="EditLayer" style="display:none;">
    <form class="layui-form" id="EditFrom" style="margin-top:10px;">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>物料信息</legend>
        </fieldset>
        <div class="layui-form-item">
            <input type="text" name="MaterialId" value="" style="display:none" />
            <div class="layui-inline">
                <label class="layui-form-label">物料编码</label>
                <div class="layui-input-inline" style="width:100px;">
                    <input type="text" name="Code" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">规格名称</label>
                <div class="layui-input-inline" style="width:300px;">
                    <input type="text" name="Specification" value="" placeholder="必填" required lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">单位</label>
                <div class="layui-input-inline" style="width:100px;">
                    <input type="text" name="Unit" value="" placeholder="必填" required lay-verify="required" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">物料类型</label>
                <div class="layui-input-inline" style="width:300px;">
                    <input type="text" name="Type" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">协议单价</label>
                <div class="layui-input-inline" style="width:100px;">
                    <input type="text" name="RecoPrice" value="" placeholder="必填" required lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">单重</label>
                <div class="layui-input-inline" style="width:100px;">
                    <input type="number" name="Weight" value="" placeholder="必填"  class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"></label>

            </div>
            <button type="button" class="layui-btn" id="BtnEditFromSave">保存</button>
        </div>
        <button type="submit" id="mysubmit" class="layui-btn" lay-submit lay-filter="formVerify" style="display:none;">立即提交</button>
    </form>
</div>

<script type="text/template" id="LayerProgBar">
    <div class="layui-progress layui-progress-big" lay-showPercent="true" lay-filter="progressBar">
        <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
    </div>
</script>

<script type="text/javascript">
    var table;
    layui.use(['form', 'table', 'upload', 'layer', 'element'], function () {
        var upload = layui.upload;
        var element = layui.element;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var form = layui.form;
        var layerindex;

        $("#BtnAdd").on("click", function () {
            //$("#addCardFrom").css("display", "block");
            //index = layer.open({
            //    type: 2,
            //    area: ['700px', '200px'],
            //    content: '/Project/InfoAdd' //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            //});
            parent.layui.index.openTabsPage('/Project/BidInfoAddView', '新增投标信息');
        });

        $("#BtnSave").on("click", function () {
            layer.close(index);
        });

        $("#BtnEditFromSave").on("click", function () {
            var obj = $("#EditFrom").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            //表示新增
            $.ajax({
                type: "POST",
                url: "/Inventory/MaterialEdit",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                        layer.close(layerindex);
                        table.reload("demo", {});

                    } else {
                        alert(msg.result);
                    }
                }
            });

        });


        ////监听单元格编辑
        //table.on('edit(UserTableList)', function (obj) {
        //    var value = obj.value //得到修改后的值
        //        , data = obj.data //得到所在行所有键值
        //        , field = obj.field; //得到字段
        //    layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
        //});

        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Inventory/MateTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "MaterialId", Order: "Desc", Condition: " and IsDelete<>1" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Code', title: '物料编码', width: 120 }
                , { field: 'Specification', title: '规格名称', width: 250 }
                , { field: 'Type', title: '物料类型', width: 150 }
                , { field: 'Unit', title: '单位', width: 75 }
                , { field: 'RecoPrice', title: '协议单价', width: 120 }
                , { field: 'Weight', title: '单重', width: 100 }
                , { field: 'LogTime', title: '更新时间', width: 120 }
                , {
                    field: "operation", title: "操作", width: 180, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="Detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });


        //鼠标双击事件回调
        //table.on('rowDouble(ProjectTableList)', function (obj) {
        //    var data = obj.data;
        //    index = layer.open({
        //        type: 2,
        //        area: ['700px','500px'],
        //        content: '/Project/InfobriefView?ProjectId=' + data.ProjectId, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
        //    });
        //    //alert(data);
        //});

        table.on("tool(ProjectTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'Detail') {

                layerindex=layer.open({
                    type: 1,
                    title: ['编辑', 'font-size:18px;'],
                    area: ['900px', '300px'],
                    content: $("#EditLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    success: function (layero, index) {
                        $("#EditFrom input[name='MaterialId']").val(data.MaterialId);
                        $("#EditFrom input[name='Code']").val(data.Code);
                        $("#EditFrom input[name='Specification']").val(data.Specification);
                        $("#EditFrom input[name='Unit']").val(data.Unit);
                        $("#EditFrom input[name='Type']").val(data.Type);
                        $("#EditFrom input[name='RecoPrice']").val(data.RecoPrice);
                        $("#EditFrom input[name='Weight']").val(data.Weight);
                    }
                });


            }

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.Specification + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Inventory/MaterialDel",
                            async: false,
                            data: { "MaterialId": data.MaterialId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });


        var uploadInst = upload.render({
            elem: '#UpLoadExcel' //绑定元素
            , url: '/Inventory/MateUpload/' //上传接口
            , accept: 'file'
            , size: 1024000
            , exts: "xls|xlsx"
            , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                LayerIndex = layer.open({
                    type: 1,
                    title: '进度条',
                    area: ['700px', '200'],
                    content: $("#LayerProgBar").html(),
                    closeBtn: 0

                });
                //然后需要重新渲染一下上传进度条
                element.init();
            }
            , progress: function (e, percent) {
                console.log("进度：" + percent + '%');
                element.progress('progressBar', percent + '%');
            }
            , done: function (msg, index, upload) {
                //上传完毕回调
                layer.close(LayerIndex);
                if (msg.result != "OK") {
                    layer.msg(msg.result, { icon: 6, time: 10000 });
                }
                else {
                    layer.msg('上传成功', { icon: 6, time: 2000 });
                    table.reload("demo", {
                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                    });
                }

            }
            , error: function () {
                //请求异常回调
                alert(msg.result);
            }
        });

    });

    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "MaterialId", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "MaterialId", Order: "Desc", Condition: " and IsDelete<>1 and Specification like '%" + text + "%'"
                },
                page: {
                    curr: 1
                }
            });
        }
    }



    function dealNumber(money) {
        if (money && money != null) {
            money = String(money);
            var left = money.split('.')[0], right = money.split('.')[1];
            right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
            var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
            return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
        } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
            return '0.00';
        } else {
            return "";
        }
    };




</script>

